<?php
/**
 *
 * @author griever
 * @version
 */
require_once 'Zend/Loader/PluginLoader.php';
require_once 'Zend/Controller/Action/Helper/Abstract.php';
require_once 'models/User.php';
require_once 'Base.php';

/**
 * CheckUsername Action Helper
 *
 * @uses actionHelper Custom_Helper
 */
class Custom_Helper_GetCrawlParser extends Dummy_Helper_Base
{

	/**
	 * Strategy pattern: call helper as broker method
	 */
	public function direct($type){
		return $this->getParser($type);
	}

	public function getParser($type)
	{
		if ($type == 'Wordpress')
			return new WordpressParser();
	}
}

require_once 'simple_html_dom.php';
require_once 'models/CrawlData.php';
class WordpressParser	{
	
	public function parseCrawlData($text)	{
		$crawlData = array();
		$dom = str_get_html($text);
		$posts = $dom->find('.post');
		foreach ($posts as $post)	{
			$entrytitle = $post->find('h2.entry-title a');
			if (empty($entrytitle))	{
				continue; 
			}
			$title = $entrytitle[0]->plaintext;
			$url = $entrytitle[0]->href;
			$summary = $post->find('div.entry-summary p');
			if (empty($summary))	{
				continue;
			}
			$summary = $summary[0]->plaintext;
			$crawl = new CrawlData($title, $url, $summary);
			$crawlData[] = $crawl;
		}
		return $crawlData;
	}
}

